import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from typing import Any


def hist(x: list[float], bins: int, color: str):
    new_x = list(range(0, bins))
    step = 1
    new_y = [0] * len(new_x)
    for m in x:
        for starting in new_x:
            if starting <= m < starting + step:
                index = starting
                new_y[index] += 1
                break
    s = sum(new_y)
    new_y = [m / s for m in new_y]
    plt.bar(new_x, new_y, color=color, edgecolor=color)


with Image.open("Tulips.jpg") as img:
    img: Any = img
    im = np.array(img)

n = int(im.size / 3)
hd = n * [0]
row = im.shape[0]
col = im.shape[1]
for i in range(row):
    for j in range(col):
        v = im[i, j, 0] * 0.299 + im[i, j, 1] * 0.587 + im[i, j, 2] * 0.114
        hd[i * col + j] = v

# plt.hist(hd, 256, color='k')
hist(hd, 256, color='k')

plt.show()
